package com.lune.job.controller;


import com.lune.job.pojo.ResPageBean;
import com.lune.job.pojo.RespBean;
import com.lune.job.pojo.UserMenu;
import com.lune.job.service.IUserMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/user-menu")
@Api(tags = "用户菜单管理")
public class UserMenuController {

    @Autowired
    private IUserMenuService userMenuService;

    @ApiOperation(value = "添加用户菜单")
    @PostMapping("/")
    public RespBean create(@RequestBody UserMenu userMenu){
        if (userMenuService.save(userMenu)){
            return RespBean.success("添加用户菜单成功");
        }
        return RespBean.error("添加用户菜单失败");

    }

    @ApiOperation(value = "删除用户菜单")
    @DeleteMapping("/")
    public RespBean delete(Integer[] ids){
        if (userMenuService.removeByIds(Arrays.asList(ids))){
            return RespBean.success("删除用户菜单成功");
        }
        return RespBean.error("删除用户菜单失败");

    }

    @ApiOperation(value = "修改用户菜单")
    @PutMapping("/")
    public RespBean update(@RequestBody UserMenu userMenu){
        if (userMenuService.updateById(userMenu)){
            return RespBean.success("修改用户菜单成功");

        }
        return RespBean.error("修改用户菜单失败");

    }

    @ApiOperation(value = "根据id查询用户菜单")
    @GetMapping("/detail")
    public UserMenu detail(Integer id){
        return  userMenuService.getById(id);
    }

    @ApiOperation(value = "查询用户菜单数量")
    @GetMapping("/count")
    public int count(UserMenu userMenu){
        return userMenuService.count();
    }

    @ApiOperation(value = "分页查询用户菜单")
    @GetMapping("/page")
    public ResPageBean queryPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                        @RequestParam(defaultValue = "10") Integer size,
                                        UserMenu userMenu){
        return userMenuService.queryPage(currentPage,size,userMenu);
    }
}
